<?php
namespace app\api\controller;

// 1. 资源及配置加载

require_once  dirname(__FILE__). "/../../../extend/BsPaySdk/init.php";
use think\Request;
use BsPaySdk\core\BsPay;
use BsPaySdk\core\BsPayTools;
use app\api\model\OrderCommon;
use app\api\model\Topup;

class Juhepaytest extends Common
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $merConfig = BsPay::init([
          "sys_id"=>"6666000108854952",
          "product_id"=>"YYZY",    
          "rsa_merch_private_key"=>"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCxtfk3rjwdpBV81WBy5jIMcDLFdvHckhjGXkmWfaBn7euPRyetEhS4inpr7EvQ5KDUXNBPljI2NVhG/LEGZKvau1MW8j3t7dJ3gWafuVGsCiLJHU79sIRHf11nKOTykX5WxB/7MMwRnZsECuaZyCk7WPuSAlznqbDJdrZTzHhjQzMhjto1qD6+vc0OxyaBFlOY9piBtEfecsvD+6GfQ8exFqwzblJm9iZPYw02DaeUDLFO9Umn7i7gShlj/1Hh8nEM7YitpF/p26o+MC9LHWbIjgzjvNVhSRVmbvWys+3S11Zm/vux6Yzfk0H3fqrksAKSEkLEtEoYKS4wKjHdecztAgMBAAECggEACy1g4WmqCks5tsJM8K0d1L5x0w2qJK9js4ZWpop8Pk0ulbJqAm6ysvCyxnr0Qc0/eFvmFjtiKRqt1LksATTvwjAqB7Vww7hDlpSi+cTUKDfy/CdFwpsJlt2h6E0gKUmRYq+vO0NUcn8xMs3ktyNpxHvSRtqzMTbxEZrP2PFxWPzUKGNyk53FTlJ64YCoGQqWeGhA5LO6QLPHlAxIrvRf9B5dtXQr5XZXVqS9MwjtsRPvQPWiFXxlzvhJRcL/wXehcNextHzpMMgX/idB3HIpIl6XXLKiFUR4rBDJIMiQjQvS6zz2l1zpiJ0vWujVa3IY+PNefRA2ttg1DeC19GYa2QKBgQDh7AkJ7wut7p4qYAdcFEDVhFgP5mnSRyOBGWmClHYE4RIFplPiv4yO0fttAjFuCg4Zaxq49BuV3zshWOEIr72VK6wMa6Z+QbfXNr/1DT6nW+ktgXTw2G9Ts/nZiMrpcsbl7qvwChfJAPvEwnyP7Ckmd9t2WbQisuYZc+Vu8znO7wKBgQDJXskTiExEipQSOcVH5cX/ExVyj9MoLjmJhy3WTTDzGafgEoOPOfej2ZCgF6gCwugXJr+rtgdOpASk8WPACaCePdjdgQ2NVhSfV3op3TtvhgAPf3iI/zCVkZM4I1iZs6KjdHstLCKyAzCFBsowkPbfZBlFX4eO7Bk6XcIZ6x2h4wKBgQDcH64C5s4bb2beZOhm2Dj/kU54V4l93+CBFjCOkXaYdG+p35DWWspqEcCHSt68l8F7FLdZxEbodTPY3w+L9iejI4UkKPN1CzVD1U2dR4VnbY85zmwRiuCVzsM/KCCE61dOi4ktfbgFGhc1dEYHuROzLo8/tlFkiajW3eyLeSM3MwKBgATL3iw57d8gEeDRQXKx9WJa+QLOjDAD0dkFwEC/e+/+Z3I93qZVsiFT+E7n4VeXfuG2SZB0eH4WCApJuZ+EWzAJtxWnkkQQjdMxyTYgD99bKLs1xRA2S9j0K7aFmQGoNrJ//sMXrwfgbZJtk/lOKqMthjCR0u/DjeJHA22MnRsTAoGADXzJs/of0JExvQWwfdIUnSEPs/PgTrrJpo+CAdXnagYHF+InrmvIcNwx6ZzIs+9aGwUt0d/YsSpJkHMfAtTwZjB7sSw8Cg5DZ179Jy3YkKhFPvZv2ZCANa5J74HZNQUrUUL6O4FouZUiLwFlq8YuUPRtkAjYwyS/jwUbhJzqZhQ=",
          // "rsa_huifu_public_key"=>"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkmP0eyQVeGHVxk/4+Zq9R6tm8vmQZaMAvtvy5D/Ki3lvhfRn8T3R3vP5xTpp2zAtmZzFnc1k4DswYBsv7lsxwseoJ3sIM6of6NYnf+39CK+NhKSsdDNDET2KCxD5Gip3WJVtgP3P4w3P/U2jCi8Xzm9sxy3XZ3TSZuokvX77UnfBZTFMuGjCmZFcuM6VjDOxoaEtnYhAVLOQItb3Jr1vvIg4vrhCWBus9YVgl3Ua5qtTccmpspnFbY3ix0UFrmHczTaPWrxKMrnEIDurgmMPXPiyR67Z/AcvcJ8vgvaVHnINmDb0hlRUkI623P9dO/fPswaNIEj0ZeoZHcdLya07swIDAQAB"
          "rsa_huifu_public_key"=>"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkMX8p3GyMw3gk6x72h20NOk3L9+Nn9mOVP6+YoBwCe7Zs4QmYrA/etFRZw2TQrSc51wgtCkJi1/x8Wl7maPL1uH2+77JFlPv7H/F4Lr2I2LXgnllg6PtwOSw/qvGYInVVB4kL85VQl0/8ObyxBUdJ43I0z/u8hJb2gwujSudOGizbeqQXAYrwcNy+e+cjodpPy9unpJjBfa4Wz2eVLLvUYYKZKdRn6pZR2cQsMBvL30K4cFlZqlJ9iP2hTG3gaiZJ9JrjTigwki0g9pbTDXiPACfuF1nOeObvLD22zBbgn1kwgfsqoG67z7g84u2jvfUFCzX1JRgd0xfNorTRkS2RQIDAQAB"
        ], true);
    }
    public function index($order_sn = '', $total_amount = 9999.99, $trade_type = 'A_NATIVE', $goods_desc = '账户充值')
    {

        $result = $this->check($order_sn);
    
        if ($result['code'] != 200) {
            return $this->miss($result['msg']);
        }
    
        $total_amount = $result['data']['total_amount'];

        $merConfig = BsPay::getConfig();
        // 2.组装请求参数
        $paramsInfo = array();

        $order_sn = $order_sn?:uniqid('cz');
        $date = date('Ymd');
        // 请求日期
        $paramsInfo["req_date"]= $date;
        // 请求流水号
        $paramsInfo["req_seq_id"]= "rQ".$date.date('His').uniqid();
        // 商户号
        $paramsInfo["huifu_id"]= "6666000109133323";
        // 商品描述
        $paramsInfo["goods_desc"]= $goods_desc;
        // 交易类型
        // T_JSAPI: 微信公众号支付
        // T_MINIAPP: 微信小程序支付
        // A_JSAPI: 支付宝JS
        // A_NATIVE: 支付宝正扫
        // U_NATIVE: 银联正扫
        // U_JSAPI: 银联 JS
        // D_NATIVE: 数字人民币正扫
        // T_H5：微信直连H5支付
        // T_APP：微信APP支付
        // T_NATIVE：微信正扫
        $paramsInfo["trade_type"]= $trade_type;
        // 交易金额
        // 单位元， 示例值：1.00单位元， 示例值：1.00
        $paramsInfo["trans_amt"]= "$total_amount";
        // 交易有效期
        $paramsInfo["time_expire"]= date('YmdHis', strtotime('+2 hour'));
        // 微信参数集合
        $paramsInfo["wx_data"]= json_encode([
            "sub_appid"=>"wxdfe9a5d141f96685",
            "sub_openid"=>"o8jhotzittQSetZ-N0Yj4Hz91Rqc",
            "attach"=>$order_sn,
            "detail"=>[
                "cost_price"=>"43.00",
                "receipt_id"=>"20220628132043853798",
                "goods_detail"=>[
                    [
                        "goods_id"=>"6934572310301",
                        "goods_name"=>"太龙双黄连口服液",
                        "price"=>"43.00",
                        "quantity"=>"1",
                        "wxpay_goods_id"=>"12235413214070356458058"
                    ]
                ]
            ]
        ], JSON_UNESCAPED_UNICODE);
        // 支付宝参数集合
        $paramsInfo["alipay_data"]= json_encode([
            "buyer_id"=>"",
            "buyer_logon_id"=>"",
            "extend_params"=>[
                "food_order_type"=>"qr_order",
                "industry_reflux_info"=>"string",
                "parking_id"=>"123wsx",
                "sys_service_provider_id"=>"1111111"
            ],
            "merchant_order_no"=>$order_sn,
            "operator_id"=>"",
            "product_code"=>"",
            "seller_id"=>""
        ], JSON_UNESCAPED_UNICODE);
        // 是否延迟交易
        $paramsInfo["delay_acct_flag"]= "N";
        // 分账对象
        $paramsInfo["acct_split_bunch"]= json_encode([
            "acct_infos"=>[
                [
                    "div_amt"=>"$total_amount",
                    "huifu_id"=>"6666000109133323"
                ]
            ]
        ], JSON_UNESCAPED_UNICODE);
        // 传入分账遇到优惠的处理规则
        $paramsInfo["term_div_coupon_type"]= "0";
        // 禁用信用卡标记
        $paramsInfo["limit_pay_type"]= "NO_CREDIT";
        // 场景类型
        $paramsInfo["pay_scene"]= "02";
        // 备注
        $paramsInfo["remark"]= $order_sn;
        // 安全信息
        $paramsInfo["risk_check_data"]= json_encode([
            "ip_addr"=>"180.167.105.130",
            "base_station"=>"192.168.1.1",
            "latitude"=>"33.3",
            "longitude"=>"33.3"
        ], JSON_UNESCAPED_UNICODE);
        // 设备信息
        $paramsInfo["terminal_device_data"]= json_encode([
            "device_type"=>"1",
            "device_ip"=>"10.10.0.1",
            "device_gps"=>"192.168.0.0",
            "devs_id"=>"SPINTP357338300264411"
        ], JSON_UNESCAPED_UNICODE);
        // 异步通知地址
        $paramsInfo["notify_url"]= url('api/Juhepay/notify', '', '', true);

        // 3. 发起API调用
        $bspay = new BsPay();
        $result = $bspay->post("v2.trade.payment.jspay", $paramsInfo, '', 'default');
        if (!$result || $result->isError()) {  //失败处理
            // var_dump($result -> getErrorInfo());
            return $this->miss($result -> getErrorInfo()['msg']);
        } else {    //成功处理
            // echo "<pre>";
            // var_dump($result);
            // echo "<br />";
            $data = $result->getRspDatas();
            // var_dump($data);
            // echo "<br />";
            if (isset($data['data']['resp_code']) && $data['data']['resp_code']!='00000100') {
                // echo "错误码：" . $data['data']['resp_code'];
                return $this->miss("错误码：" . $data['data']['resp_code']);
            }
            // if (isset($data['data']['pay_info'])) {
            //     var_dump($data['data']['pay_info']);
            // }

            return $this->pass($data);
        }
    }

    public function notifybak()
    {
        $merConfig = BsPay::getConfig();

        $data = "{\"acct_date\":\"20250218\",\"acct_id\":\"F00300982\",\"acct_split_bunch\":{\"acct_infos\":[{\"acct_date\":\"20250218\",\"acct_id\":\"F00300982\",\"div_amt\":\"0.02\",\"huifu_id\":\"6666000109133323\"}],\"fee_acct_date\":\"20250218\",\"fee_acct_id\":\"F00300982\",\"fee_amt\":\"0.01\",\"fee_huifu_id\":\"6666000109133323\"},\"acct_stat\":\"S\",\"alipay_response\":{\"app_id\":\"\",\"buyer_id\":\"2088802931474756\",\"buyer_logon_id\":\"180****4207\",\"coupon_fee\":\"0.00\",\"fund_bill_list\":[{\"amount\":\"0.02\",\"fund_channel\":\"ALIPAYACCOUNT\"}]},\"atu_sub_mer_id\":\"2088310451692591\",\"avoid_sms_flag\":\"\",\"bagent_id\":\"6666000108840829\",\"bank_code\":\"TRADE_SUCCESS\",\"bank_desc\":\"TRADE_SUCCESS\",\"bank_message\":\"TRADE_SUCCESS\",\"bank_order_no\":\"982025021822001474751404904380\",\"bank_seq_id\":\"1d5fce\",\"base_acct_id\":\"F00300982\",\"batch_id\":\"250218\",\"channel_type\":\"U\",\"combinedpay_data\":[],\"combinedpay_fee_amt\":\"0.00\",\"debit_type\":\"0\",\"delay_acct_flag\":\"N\",\"devs_id\":\"SPINTP357338300264411\",\"div_flag\":\"1\",\"end_time\":\"20250218161029\",\"fee_amount\":\"0.01\",\"fee_amt\":\"0.01\",\"fee_flag\":2,\"fee_formula_infos\":[{\"fee_formula\":\"MAX(0.01,AMT*0.0038)\",\"fee_type\":\"TRANS_FEE\"}],\"fee_rec_type\":\"1\",\"fee_type\":\"INNER\",\"gate_id\":\"Vu\",\"hf_seq_id\":\"002900TOP3A250218160939P212ac1360b800000\",\"huifu_id\":\"6666000109133323\",\"is_delay_acct\":\"0\",\"is_div\":\"1\",\"maze_resp_code\":\"\",\"mer_name\":\"上海汇涵信息科技服务有限公司\",\"mer_ord_id\":\"rQ2025021816093767b44041d5fce\",\"mypaytsf_discount\":\"0.00\",\"need_big_object\":true,\"notify_type\":2,\"org_auth_no\":\"\",\"org_huifu_seq_id\":\"\",\"org_trans_date\":\"\",\"out_ord_id\":\"982025021822001474751404904380\",\"out_trans_id\":\"982025021822001474751404904380\",\"party_order_id\":\"03242502185817944602595\",\"pay_amt\":\"0.02\",\"pay_scene\":\"02\",\"posp_seq_id\":\"03242502185817944602595\",\"product_id\":\"YYZY\",\"ref_no\":\"1609391d5fce\",\"remark\":\"string\",\"req_date\":\"20250218\",\"req_seq_id\":\"rQ2025021816093767b44041d5fce\",\"resp_code\":\"00000000\",\"resp_desc\":\"交易成功\",\"risk_check_data\":{\"ip_addr\":\"10.10.0.1\",\"latitude\":\"33.3\",\"longitude\":\"33.3\"},\"risk_check_info\":{\"client_ip\":\"10.10.0.1\",\"latitude\":\"33.3\",\"lc\":\"192.168.1.1\",\"longitude\":\"33.3\"},\"settlement_amt\":\"0.02\",\"sub_resp_code\":\"00000000\",\"sub_resp_desc\":\"交易成功\",\"subsidy_stat\":\"I\",\"sys_id\":\"6666000108854952\",\"terminal_device_data\":{\"device_gps\":\"192.168.0.0\",\"device_ip\":\"10.10.0.1\",\"device_type\":\"1\"},\"terminal_device_info\":{\"device_gps\":\"192.168.0.0\",\"device_ip\":\"10.10.0.1\",\"device_type\":\"1\",\"devs_id\":\"SPINTP357338300264411\"},\"trade_type\":\"A_NATIVE\",\"trans_amt\":\"0.02\",\"trans_date\":\"20250218\",\"trans_order_info\":{\"acct_fee_date\":\"20250218\",\"acct_finish_time\":1739866230000,\"acct_id\":\"F00300982\",\"acct_stat\":\"S\",\"acct_trans_date\":\"20250218\",\"agent_id\":\"6666000108840829\",\"atu_sub_mer_id\":\"2088310451692591\",\"bagent_id\":\"6666000108840829\",\"bank_mer_id\":\"A1035134718836157663\",\"bank_mer_name\":\"上海汇涵信息科技服务有限公司\",\"bank_resp_code\":\"TRADE_SUCCESS\",\"bank_resp_desc\":\"TRADE_SUCCESS\",\"bank_seq_id\":\"1d5fce\",\"batch_id\":\"250218\",\"card_channel_type\":\"\",\"card_sign\":\"\",\"cash_req_date\":\"20250218160939\",\"cash_resp_code\":\"000\",\"cash_resp_desc\":\"成功\",\"cash_trans_id\":\"20250218242oi18m\",\"cashier_version\":\"V2\",\"channel_code\":\"00\",\"channel_finish_time\":1739866229000,\"channel_message\":\"TRADE_SUCCESS\",\"channel_stat\":\"S\",\"channel_type\":\"U\",\"check_cash_flag\":\"I\",\"close_trans_stat\":\"\",\"create_time\":1739866179000,\"creator\":\"\",\"credit_fee_amt\":0.00,\"credit_type\":\"\",\"db_unit\":\"3\",\"debit_fee_amt\":0.00,\"devs_id\":\"SPINTP357338300264411\",\"double_limit_amt\":0.00,\"fee_acct_id\":\"F00300982\",\"fee_amt\":0.01,\"fee_flag\":2,\"fee_formula\":\"\",\"fee_huifu_id\":\"6666000109133323\",\"fee_real_acct_id\":\"F00300982\",\"fee_real_cust_id\":\"6666000109133323\",\"fee_rec_type\":1,\"fee_source\":\"'SERVER'\",\"fee_split_type\":\"\",\"gate_id\":\"SPIN002\",\"goods_desc\":\"账户充值\",\"hf_seq_id\":\"002900TOP3A250218160939P212ac1360b800000\",\"huifu_id\":\"6666000109133323\",\"icc_data\":\"\",\"id\":4376090342,\"is_acct_div\":1,\"is_acct_div_param\":1,\"is_delay_acct\":0,\"is_deleted\":0,\"is_route\":\"\",\"iss_inst_id\":\"\",\"maze_bg_date\":\"\",\"maze_bg_seq_id\":\"\",\"maze_pnr_dev_id\":\"\",\"maze_resp_code\":\"\",\"maze_resp_desc\":\"\",\"mcc\":\"\",\"mer_name\":\"上海汇涵信息科技服务有限公司\",\"mer_ord_id\":\"rQ2025021816093767b44041d5fce\",\"modifier\":\"\",\"modify_time\":1739866230000,\"mypaytsf_discount\":0.00,\"ord_amt\":0.02,\"ord_id\":\"202502181609390TOP3_AL2093358253\",\"org_acct_id\":\"F00300982\",\"org_auth_no\":\"\",\"org_huifu_seq_id\":\"\",\"org_trans_date\":\"\",\"out_trans_id\":\"982025021822001474751404904380\",\"pa_mer_id\":\"SSP001\",\"pa_trans_id\":\"Yhvhet81biig\",\"party_order_id\":\"03242502185817944602595\",\"pay_amt\":0.02,\"pay_channel\":\"A\",\"pay_channel_id\":\"10000001\",\"pay_scene\":\"02\",\"pay_type\":\"NATIVE\",\"product_id\":\"YYZY\",\"real_acct_id\":\"F00300982\",\"real_cust_id\":\"6666000109133323\",\"real_gate_id\":\"Vu\",\"ref_amt\":0.02,\"ref_cnt\":0,\"ref_fee_amt\":0.01,\"ref_num\":\"1609391d5fce\",\"region_id\":\"TOP3_A\",\"remark\":\"string\",\"req_date\":\"20250218\",\"req_seq_id\":\"rQ2025021816093767b44041d5fce\",\"route_region_id\":\"C24_B\",\"settle_amt\":0.02,\"settle_trans_stat\":\"\",\"sn_code\":\"\",\"source_region_id\":\"TOP3_A\",\"subsidy_amt\":0.00,\"subsidy_ref_amt\":0.00,\"subsidy_stat\":\"I\",\"sys_id\":\"6666000108854952\",\"sys_trace_audit_num\":\"\",\"term_div_coupon_type\":0,\"time_expire\":\"20250218180937\",\"trans_date\":\"20250218\",\"trans_finish_time\":1739866230000,\"trans_notify_url\":\"VIRGO://http://ailiao241209.dev.relang.tv/api/juhepay/notify\",\"trans_stat\":\"S\",\"trans_type\":\"1000\",\"un_scene_info\":\"\",\"unconfirm_amt\":0.01,\"unconfirm_fee_amt\":0.01,\"version\":4},\"trans_stat\":\"S\",\"trans_time\":\"160939\",\"trans_type\":\"A_NATIVE\",\"ts_encash_detail\":[]}";
        $sign = "DrTalW9xtcFkpPbyn7IxFc4Z8NSsbq/GxP1/CtGMla1L0RO8lNLvtoJi5AtzggZjr50oKgRejoEKMBijP/R/QfA/KtuhB0jyqAIlVKOgwU2uUcNn2wkj37ABwuphBPt36V1HrkSRwp0opzei/K7YDrq4x568AdW5fymShSbkMTilXi+JZOvmcVB/PGOEWBe7L0bXECcbT+FgSMiK25KcyFUx4d3soOD5BHWWDlwYrMtaLeFMTNbNL0HGU3QT27SdaQU8NXVu2fJZF3VqNFDPU+iQ/DFuWdM14uZVgJ1M58lK+Hk3sBbmFOBS1GyxX6PPQw78iLz+v/lYF3sUxzeJrA==";
        $result = BsPayTools::verifySign($sign,$data, $merConfig->rsa_huifu_public_key);
        // var_dump($merConfig->rsa_huifu_public_key);exit;

        if (!$result) {  //失败处理
            var_dump("验签失败");
        } else {    //成功处理
            echo "<pre>";
            echo "<br />";
            $data = json_decode($data, true);
            // var_dump($data['req_seq_id']);
            halt($data);
        }
    }

    public function notify()
    {
        $merConfig = BsPay::getConfig();

        $data = input('resp_data')?:input('data');
        $sign = input('sign');
        $result = BsPayTools::verifySign($sign,$data, $merConfig->rsa_huifu_public_key);

        if (!$result) {  //失败处理
            var_dump("验签失败");
        } else {    //成功处理
            $data = json_decode($data, true);
            if (isset($data['wx_response'])) {
                $order_info = $data['wx_response'];
                $order_sn = $order_info['attach']??$data['remark'];
            } else if (isset($data['alipay_response'])) {
                $order_info = $data['alipay_response'];
                $order_sn = $order_info['merchant_order_no']??$data['remark'];
            }
            $result = OrderCommon::notify($order_sn, $order_info);
            return "RECV_ORD_ID_" . $data['req_seq_id'];
        }
    }
    private function check($out_trade_no)
    {
        $state = 0;
        
        $data = (new Topup)->master()->where(['numbers' => $out_trade_no, 'state' => 0])->find();
        
        if ($data) {
            $state = 1;
            $money = $data['price'];
        }
        
        if (!$state) {
            $data = (new OrderCommon)->master()->where(['number' => $out_trade_no, 'status' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['money'];
            }
        }

        if (!$state) {
            $data = Db::name('mall_order')->master()->where(['number' => $out_trade_no, 'status' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['money'];
            }
        }
        
        if (!$state) {
            $data = Db::name('order_of_service')->master()->where(['number' => $out_trade_no, 'paystate' => 0, 'state' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }
        
        if (!$state) {
            $data = Db::name('user_flag')->master()->where(['number' => $out_trade_no, 'paystate' => 0, 'status' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }

        if (!$state) {
            $data = Db::name('user_address_red_packet')->master()->where(['number' => $out_trade_no, 'type' => 1, 'paystate' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['totalprice'];
            }
        }

        if (!$state) {
            $data = Db::name('order_of_addressgood')->master()->where(['number' => $out_trade_no, 'status' => 0, 'paystate' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }
        
        if (!$state) {
            $data = Db::name('activity_join')->master()->where(['number' => $out_trade_no, 'paystate' => 0])->find();
            
            if ($data) {
                $state = 1;
                $money = $data['price'];
            }
        }
        
        if (!$state) {
            return ['code' => 400, 'msg' => '订单不存在'];
        } else {
            return ['code' => 200, 'data' => ['total_amount' => $money]];
        }
    }

    public function refund()
    {
        $data = db('user_request_log')->where(['request_url'=>'api/juhepay/notify'])->order('id desc')->select();
        foreach ($data as $key => $value) {
            try {
                $request_param = json_decode($value['request_param'], true);
                $resp_data = json_decode($request_param['resp_data'], true);

                $merConfig = BsPay::getConfig();
                // 2.组装请求参数
                $paramsInfo = array();
                // 请求日期
                $paramsInfo["req_date"]= "20250218";
                // 请求流水号
                $paramsInfo["req_seq_id"]= $resp_data['req_seq_id'];
                // 商户号
                $paramsInfo["huifu_id"]= "6666000109133323";
                // 申请退款金额
                $paramsInfo["ord_amt"]= "0.02";
                // 原交易请求日期
                $paramsInfo["org_req_date"]= "20250218";
                // 原交易全局流水号
                $paramsInfo["org_hf_seq_id"]= $resp_data['hf_seq_id'];
                // halt($paramsInfo);

                // 3. 发起API调用
                $bspay = new BsPay();
                $result = $bspay->post("v2.trade.payment.scanpay.refund", $paramsInfo, '', 'default');
                if (!$result || $result->isError()) {  //失败处理
                    var_dump($result -> getErrorInfo());
                } else {    //成功处理
                    echo "<pre>";
                    var_dump($result);
                }
            } catch (\Exception $e) {
                
            }
        }

    }
}